/**
 * 
 */
package com.shine.icis.blo.imp;

import java.util.List;
import java.util.Date; 
import java.text.SimpleDateFormat;

import com.shine.cotrun.exp.Export;
import com.shine.cotrun.exp.ExportConf;
import com.shine.cotrun.exp.ExportFactory;
import com.shine.cotrun.sql.QueryParameters;
import com.shine.cotrun.web.comm.ListPage;
import com.shine.icis.blo.IDataCheckService;
import com.shine.icis.dao.IDataCheckDAO;
import com.shine.icis.dao.IETLContextVarDAO;
import com.shine.icis.dto.DataCheck;
import com.shine.icis.hbean.DataCheckDef;
import com.shine.icis.hbean.DataCheckR08;
import com.shine.icis.hbean.DataCheckR09;
import com.shine.icis.hbean.DataCheckR10;
import com.shine.icis.hbean.DataCheckR11;
import com.shine.icis.hbean.DataCheckR12;
import com.shine.icis.hbean.DataCheckR13;
import com.shine.icis.hbean.ETLContextVar;
import com.shine.icis.hbean.R12ChkItem;
import com.shine.icis.hbean.R13ChkFactAmt;
import com.shine.icis.hbean.SettleAmtDetail;

/**
 * @author zsy
 *
 */
public class DataCheckService implements IDataCheckService {
	
	private IDataCheckDAO dataCheckDAO;
	private IETLContextVarDAO etlContextVarDAO;

	public void setDataCheckDAO(IDataCheckDAO dataCheckDAO) {
		this.dataCheckDAO = dataCheckDAO;
	}

	public void setEtlContextVarDAO(IETLContextVarDAO etlContextVarDAO) {
		this.etlContextVarDAO = etlContextVarDAO;
	}


	@Override
	public List<DataCheckDef> getDataCheckDefs(char enableFlag) {
		return dataCheckDAO.findDataCheckDefs(enableFlag);
	}

	@Override
	public ListPage<DataCheckR08> getDataCheckR08Page(QueryParameters qp) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		try {
			ETLContextVar var = etlContextVarDAO.findByName("BUSI_DATE");
			qp.addParameter("sjrq", Integer.parseInt(var.getVarValue()));
		} catch(Exception e) {
			e.printStackTrace();
		}
		return dataCheckDAO.findDataCheckR08Page(qp);
	}
	
	/**
	 * 资金账户完整性核对
	 * @author zhangxr 2010-11-16
	 * @param qp
	 * */	
	@Override
	public ListPage<DataCheckR09> getDataCheckR09Page(QueryParameters qp) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		try {
			ETLContextVar var = etlContextVarDAO.findByName("BUSI_DATE");
			qp.addParameter("sjrq", Integer.parseInt(var.getVarValue()));
		} catch(Exception e) {
			e.printStackTrace();
		}
		return dataCheckDAO.findDataCheckR09Page(qp);
	}
	
	/**
	 * 账户一致性核对
	 * @author zhangxr 2010-10-27
	 * @param qp
	 * */	
	@Override
	public ListPage<DataCheckR10> getDataCheckR10Page(QueryParameters qp) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		try {
			ETLContextVar var = etlContextVarDAO.findByName("BUSI_DATE");
			qp.addParameter("sjrq", Integer.parseInt(var.getVarValue()));
		} catch(Exception e) {
			e.printStackTrace();
		}
		return dataCheckDAO.findDataCheckR10Page(qp);
	}
	
	/**
	 * 客户资金结算勾稽核对
	 * @author zhangxr 2010-10-27
	 * @param qp
	 * */	
	@Override
	public ListPage<DataCheckR11> getDataCheckR11Page(QueryParameters qp) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		try {
			ETLContextVar var = etlContextVarDAO.findByName("BUSI_DATE");
			qp.addParameter("sjrq", Integer.parseInt(var.getVarValue()));
		} catch(Exception e) {
			e.printStackTrace();
		}
		return dataCheckDAO.findDataCheckR11Page(qp);
	}
	
	@Override
	public ListPage<DataCheckR12> getDataCheckR12Page(QueryParameters qp) {
		return dataCheckDAO.findDataCheckR12Page(qp);
	}
	
	@Override
	public DataCheckR12 getDataCheckR12() {
		ETLContextVar var = etlContextVarDAO.findByName("BUSI_DATE");
		int busiDate = Integer.parseInt(var.getVarValue());
		return dataCheckDAO.findDataCheckR12(busiDate);
	}
	
	@Override
	public DataCheckR12 modifyDataCheckR12Note(int id, Integer relateid, double accSumAmt, double bankAmt, 
			double reservesAmt, double bailA13Amt, double bailSdcAmt, double arapAmt, double settleAmt, 
			double fullComm, double bankInterest, double sdcCommAmt, double secInterest, double bankComm, 
			double sqtClearAmt, double sqtStamptax, double discAmt, String note) {
		DataCheckR12 r12 = null;
		if (relateid != null) {
			r12 = dataCheckDAO.findDataCheckR12ById(relateid);
		}
		if (r12 == null){
			r12 = new DataCheckR12();
			DataCheckR12 or12 = dataCheckDAO.findDataCheckR12ById(id);
			if (or12 == null) {
				throw new IllegalArgumentException();
			}
			r12.setBusiDate(or12.getBusiDate());
			r12.setOperDate(or12.getOperDate());
			r12.setOperTime(or12.getOperTime());
			r12.setRelateObj(or12);
		}
		r12.setAccSumAmt(accSumAmt);
		r12.setBankAmt(bankAmt);
		r12.setReservesAmt(reservesAmt);
		r12.setBailA13Amt(bailA13Amt);
		r12.setBailSdcAmt(bailSdcAmt);
		r12.setArapAmt(arapAmt);
		r12.setSettleAmt(settleAmt);
		r12.setFullComm(fullComm);
		r12.setBankInterest(bankInterest);
		r12.setSdcCommAmt(sdcCommAmt);
		r12.setSecInterest(secInterest);
		r12.setBankComm(bankComm);
		r12.setSqtClearAmt(sqtClearAmt);
		r12.setSqtStamptax(sqtStamptax);
		r12.setDiscAmt(discAmt);
		r12.setNote(note);
		dataCheckDAO.saveOrUpdate(r12);
		
		return r12;
	}
	
	@Override
	public List<R12ChkItem> getManuChkItems() {
		return dataCheckDAO.findChkItems(R12ChkItem.FLAG_MANU);
	}
	@Override
	public ListPage<DataCheckR13> getDataCheckR13Page(QueryParameters qp) {
		return dataCheckDAO.findDataCheckR13Page(qp);
	}
	
	@Override
	public DataCheckR13 getDataCheckR13(int queryDate) {
		//ETLContextVar var = etlContextVarDAO.findByName("BUSI_DATE");
		//int busiDate = Integer.parseInt(var.getVarValue());
		return dataCheckDAO.findDataCheckR13(queryDate);
	}
	
	@Override
	public DataCheckR13 modifyDataCheckR13Note(int id, Integer relateid, String chkType, double discAmt,
			double reservesFactAmt, double fullComm,double accumAdjustAmt, double sdcCommAmt,double bankComm,
			double bankInterest,double reservesInterest,double secInterest,double withheldTaxAmt, double settleAdjustAmt, 
			double clearOffsetBal, double stamptaxOffsetBal, double redBlueFillAmt, double other1Amt, double other2Amt, 
			double other3Amt, double other4Amt, String note) {
		DataCheckR13 r13 = null;
		if (relateid != null) {
			r13 = dataCheckDAO.findDataCheckR13ById(relateid);
		}
		if (r13 == null){
			r13 = new DataCheckR13();
			DataCheckR13 or13 = dataCheckDAO.findDataCheckR13ById(id);
			if (or13 == null) {
				throw new IllegalArgumentException();
			}
			r13.setBusiDate(or13.getBusiDate());
			r13.setOperDate(or13.getOperDate());
			r13.setOperTime(or13.getOperTime());
			r13.setRelateObj(or13);
		}
		r13.setChkType(chkType);
		r13.setDiscAmt(discAmt);
		r13.setReservesFactAmt(reservesFactAmt);
		r13.setFullComm(fullComm);
		r13.setAccumAdjustAmt(accumAdjustAmt);
		r13.setSdcCommAmt(sdcCommAmt);
		r13.setBankComm(bankComm);
		r13.setBankInterest(bankInterest);
		r13.setReservesInterest(reservesInterest);
		r13.setSecInterest(secInterest);
		r13.setWithheldTaxAmt(withheldTaxAmt);
		r13.setSettleAdjustAmt(settleAdjustAmt);
		r13.setClearOffsetBal(clearOffsetBal);
		r13.setStamptaxOffsetBal(stamptaxOffsetBal);
		r13.setRedBlueFillAmt(redBlueFillAmt);
		r13.setOther1Amt(other1Amt);
		r13.setOther2Amt(other2Amt);
		r13.setOther3Amt(other3Amt);
		r13.setOther4Amt(other4Amt);
		r13.setNote(note);
		dataCheckDAO.saveOrUpdate(r13);
		
		return r13;
	}
	@Override
	public R13ChkFactAmt getR13ChkFactAmt(int queryDate) {
		//ETLContextVar var = etlContextVarDAO.findByName("BUSI_DATE");
		//int busiDate = Integer.parseInt(var.getVarValue());
		return dataCheckDAO.findChkFactAmt (queryDate);
	}
	@Override
	public SettleAmtDetail getSettleAmtDetail(String dateType,int queryDate) {
		//ETLContextVar var = etlContextVarDAO.findByName("BUSI_DATE");
		//int busiDate = Integer.parseInt(var.getVarValue());
		return dataCheckDAO.findSettleAmtDetail(dateType,queryDate);
	}
	@Override
	public R13ChkFactAmt modifyR13ChkFactAmt(int queryDate, double shFactAmt, double szFactAmt ) {
		R13ChkFactAmt r13ChkFactAmt = new R13ChkFactAmt();
		
		r13ChkFactAmt.setBusiDate(queryDate);
		r13ChkFactAmt.setShFactAmt(shFactAmt);
		r13ChkFactAmt.setSzFactAmt(szFactAmt);
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式
		r13ChkFactAmt.setOperDate(Integer.parseInt(df.format(new Date())));
		df = new SimpleDateFormat("HHmmss");//设置日期格式
		r13ChkFactAmt.setOperTime(Integer.parseInt(df.format(new Date())));
		r13ChkFactAmt.setNote("");
		dataCheckDAO.saveOrUpdate(r13ChkFactAmt);
		
		return r13ChkFactAmt;
	}
	
	@Override
	public String csExpDataCheckR08(QueryParameters qp, String dir, String expType) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		//初始化配置信息
		ExportConf cfg = new ExportConf();
		cfg.setExpToDir(dir);
		cfg.setExpFileName("客户资金账户资金连续性核对结果");
		cfg.addField("交易日期", "formatTradeDate", String.class, 10);
		cfg.addField("营业部名称", "depName", String.class, 120);
		cfg.addField("客户资金账户", "fundAcct", String.class, 20);
		cfg.addField("币种类别", "curTypeSipfName", String.class, 15);
		cfg.addField("T日客户资金账户余额", "currFundBal", Double.class);
		cfg.addField("T日客户资金变动金额", "totalChgBal", Double.class);
		cfg.addField("T-1日资金帐户余额", "lastFundBal", Double.class);
		cfg.addField("差额", "discAmt", Double.class);
		Export<DataCheckR08> exp = ExportFactory.createInstance(expType, cfg);

		try {
			exp.init();//初始化工作
			
			//添加额处查询条件
			ETLContextVar var = etlContextVarDAO.findByName("BUSI_DATE");
			qp.addParameter("sjrq", Integer.parseInt(var.getVarValue()));
			qp.setPageNo(1);
			qp.setPageSize(10000);
			
			ListPage<DataCheckR08> page = dataCheckDAO.findDataCheckR08Page(qp);//查第一页
			//第一页写到文件
			exp.export(page.getDataList());
			if (page.getTotalPageCount() > 1) {
				for (int i = 2; i <= page.getTotalPageCount(); i++) {
					qp.setPageNo(i);
					page = dataCheckDAO.findDataCheckR08Page(qp);//查第i页
					//第i页写到文件
					exp.export(page.getDataList());
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			//结束写入
			exp.end();
		}
		return exp.getFileName();//返回文件名（含扩展名）
	}
	
	@Override
	public String csExpDataCheckR09(QueryParameters qp, String dir, String expType) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		//初始化配置信息
		ExportConf cfg = new ExportConf();
		cfg.setExpToDir(dir);
		cfg.setExpFileName("资金账户完整性核对结果");
		cfg.addField("营业部名称", "depName", String.class, 120);
		cfg.addField("客户资金账户", "fundAcct", String.class, 20);
		cfg.addField("异常说明", "compareDescText", String.class, 50);
		cfg.addField("检查情况", "compareFlagText", String.class, 10);

		Export<DataCheckR09> exp = ExportFactory.createInstance(expType, cfg);
		
		try {
			exp.init();//初始化工作
			
			//添加额处查询条件
			ETLContextVar var = etlContextVarDAO.findByName("BUSI_DATE");
			qp.addParameter("sjrq", Integer.parseInt(var.getVarValue()));
			qp.setPageNo(1);
			qp.setPageSize(10000);
			
			ListPage<DataCheckR09> page = dataCheckDAO.findDataCheckR09Page(qp);//查第一页
			//第一页写到文件
			exp.export(page.getDataList());
			if (page.getTotalPageCount() > 1) {
				for (int i = 2; i <= page.getTotalPageCount(); i++) {
					qp.setPageNo(i);
					page = dataCheckDAO.findDataCheckR09Page(qp);//查第i页
					//第i页写到文件
					exp.export(page.getDataList());
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			//结束写入
			exp.end();
		}
		return exp.getFileName();//返回文件名（含扩展名）
	}
	
	@Override
	public String csExpDataCheckR10(QueryParameters qp, String dir, String expType) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		//初始化配置信息
		ExportConf cfg = new ExportConf();
		cfg.setExpToDir(dir);
		cfg.setExpFileName("资金账户一致性核对结果");
		cfg.addField("营业部名称", "depName", String.class, 120);
		cfg.addField("客户资金账户", "fundAcct", String.class, 20);
		cfg.addField("证券账户账号", "invAcct", String.class, 20);
		cfg.addField("历史对应证券账户账号", "oldInvAcct", String.class, 20);
		cfg.addField("证券账户类别", "invAcctTypeName", String.class, 80);
		cfg.addField("是否一致", "compareFlagText", String.class, 30);
		Export<DataCheckR10> exp = ExportFactory.createInstance(expType, cfg);
		
		try {
			exp.init();//初始化工作
			
			//添加额处查询条件
			ETLContextVar var = etlContextVarDAO.findByName("BUSI_DATE");
			qp.addParameter("sjrq", Integer.parseInt(var.getVarValue()));
			qp.setPageNo(1);
			qp.setPageSize(10000);
			
			ListPage<DataCheckR10> page = dataCheckDAO.findDataCheckR10Page(qp);//查第一页
			//第一页写到文件
			exp.export(page.getDataList());
			if (page.getTotalPageCount() > 1) {
				for (int i = 2; i <= page.getTotalPageCount(); i++) {
					qp.setPageNo(i);
					page = dataCheckDAO.findDataCheckR10Page(qp);//查第i页
					//第i页写到文件
					exp.export(page.getDataList());
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			//结束写入
			exp.end();
		}
		return exp.getFileName();//返回文件名（含扩展名）
	}
	
	@Override
	public String csExpDataCheckR11(QueryParameters qp, String dir, String expType) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		//初始化配置信息
		ExportConf cfg = new ExportConf();
		cfg.setExpToDir(dir);
		cfg.setExpFileName("客户资金结算勾稽核对结果");
		cfg.addField("交易日期", "formatTradeDate", String.class, 10);
		cfg.addField("营业部名称", "depName", String.class, 120);
		cfg.addField("客户资金账户", "fundAcct", String.class, 20);
		cfg.addField("发生金额", "currFundBal", Double.class);
		cfg.addField("清算金额", "lastFundBal", Double.class);
		cfg.addField("印花税", "exchAmt", Double.class);
		cfg.addField("T-过户费", "interestAmt", Double.class);
		cfg.addField("手续费", "bondExtrAmt", Double.class);
		cfg.addField("其它费用1", "totalChgBal", Double.class);
		cfg.addField("其它费用2", "totalChgBal2", Double.class);
		cfg.addField("其它费用3", "totalChgBal3", Double.class);
		cfg.addField("差额", "discAmt", Double.class);
		Export<DataCheckR11> exp = ExportFactory.createInstance(expType, cfg);
		
		try {
			exp.init();//初始化工作
			
			//添加额处查询条件
			ETLContextVar var = etlContextVarDAO.findByName("BUSI_DATE");
			qp.addParameter("sjrq", Integer.parseInt(var.getVarValue()));
			qp.setPageNo(1);
			qp.setPageSize(10000);
			
			ListPage<DataCheckR11> page = dataCheckDAO.findDataCheckR11Page(qp);//查第一页
			//第一页写到文件
			exp.export(page.getDataList());
			if (page.getTotalPageCount() > 1) {
				for (int i = 2; i <= page.getTotalPageCount(); i++) {
					qp.setPageNo(i);
					page = dataCheckDAO.findDataCheckR11Page(qp);//查第i页
					//第i页写到文件
					exp.export(page.getDataList());
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			//结束写入
			exp.end();
		}
		return exp.getFileName();//返回文件名（含扩展名）
	}
	
	@Override
	public String csExpDataCheckR12(QueryParameters qp, String dir, String expType) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		//初始化配置信息
		ExportConf cfg = new ExportConf();
		cfg.setExpToDir(dir);
		cfg.setExpFileName("客户交易结算资金余额核对结果");
		cfg.addField("生成时间", "formatOperTime", String.class, 20);
		cfg.addField("客户资金账户汇总余额", "accSumAmtNote", String.class, 40);
		cfg.addField("客户资金专用银行存款", "bankAmtNote", String.class, 40);
		cfg.addField("客户结算备付金", "reservesAmtNote", String.class, 40);
		cfg.addField("客户交易保证金(A13)", "bailA13AmtNote", String.class, 40);
		cfg.addField("客户交易保证金(中登)", "bailSdcAmtNote", String.class, 40);
		cfg.addField("应收应付金额", "arapAmtNote", String.class, 40);
		cfg.addField("应交收金额(不含三板数据)", "settleAmtNote", String.class, 40);
		cfg.addField("证券公司向客户扣收的手续费", "fullCommNote", String.class, 40);
		cfg.addField("证券公司客户资金存款账户收到的利息", "bankInterestNote", String.class, 40);
		cfg.addField("证券公司支付登记公司的一级费用(不含三板数据)", "sdcCommAmtNote", String.class, 40);
		cfg.addField("证券公司向客户支付的结息金额", "secInterestNote", String.class, 40);
		cfg.addField("银行收取的手续费", "bankCommNote", String.class, 40);
		cfg.addField("清算金额(三板)", "sqtClearAmtNote", String.class, 40);
		cfg.addField("印花税(三板)", "sqtStamptaxNote", String.class, 40);
		cfg.addField("挪用金额", "discAmtNote", String.class, 40);
		Export<DataCheckR12> exp = ExportFactory.createInstance(expType, cfg);
		try {
			exp.init();//初始化工作
			
			qp.setPageNo(1);
			qp.setPageSize(10000);
			
			ListPage<DataCheckR12> page = dataCheckDAO.findDataCheckR12Page(qp);//查第一页
			//第一页写到文件
			exp.export(page.getDataList());
			if (page.getTotalPageCount() > 1) {
				for (int i = 2; i <= page.getTotalPageCount(); i++) {
					qp.setPageNo(i);
					page = dataCheckDAO.findDataCheckR12Page(qp);//查第i页
					//第i页写到文件
					exp.export(page.getDataList());
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			//结束写入
			exp.end();
		}
		return exp.getFileName();//返回文件名（含扩展名）
	}
	@Override
	public String csExpDataCheckR13(QueryParameters qp, String dir, String expType) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		//初始化配置信息
		ExportConf cfg = new ExportConf();
		cfg.setExpToDir(dir);
		cfg.setExpFileName("客户自有两类资金划转核对");
		cfg.addField("生成时间", "formatOperTime", String.class, 20);
		cfg.addField("核对类型", "chkType", String.class, 40);
		cfg.addField("挪用金额", "discAmtNote", String.class, 40);
		cfg.addField("T日客户自有资金实际划转金额", "reservesFactAmtNote", String.class, 40);
		cfg.addField("T-1日证券公司向客户扣收的手续费", "fullCommNote", String.class, 40);
		cfg.addField("T-1日累计待调整自有收支", "accumAdjustAmtNote", String.class, 40);
		cfg.addField("T-1证券公司支付登记公司的一级费用(不含三板数据）", "sdcCommAmtNote", String.class, 40);
		cfg.addField("T-1日银行收取的手续费", "bankCommNote", String.class, 40);
		cfg.addField("T-1证券公司客户资金存款账户收到的利息", "bankInterestNote", String.class, 40);
		cfg.addField("T-1证券公司客户资金存款账户收到的利息", "reservesInterestNote", String.class, 40);
		cfg.addField("T-1证券公司向客户支付的结息金额", "secInterestNote", String.class, 40);
		cfg.addField("T-1日当日代扣代缴税费", "withheldTaxAmtNote", String.class, 40);
		cfg.addField("T日当日交收当日已调整自有资金", "settleAdjustAmtNote", String.class, 40);
		cfg.addField("T-1日清算轧差", "clearOffsetBalNote", String.class, 40);
		cfg.addField("T-1日印花税轧差", "stamptaxOffsetBalNote", String.class, 40);
		cfg.addField("T-1日红冲蓝补", "redBlueFillAmtNote", String.class, 40);
		cfg.addField("其他费用1", "other1AmtNote", String.class, 40);
		cfg.addField("其他费用2", "other2AmtNote", String.class, 40);
		cfg.addField("其他费用3", "other3AmtNote", String.class, 40);
		cfg.addField("其他费用4", "other4AmtNote", String.class, 40);
		
		Export<DataCheckR13> exp = ExportFactory.createInstance(expType, cfg);
		try {
			exp.init();//初始化工作
			
			qp.setPageNo(1);
			qp.setPageSize(10000);
			
			ListPage<DataCheckR13> page = dataCheckDAO.findDataCheckR13Page(qp);//查第一页
			//第一页写到文件
			exp.export(page.getDataList());
			if (page.getTotalPageCount() > 1) {
				for (int i = 2; i <= page.getTotalPageCount(); i++) {
					qp.setPageNo(i);
					page = dataCheckDAO.findDataCheckR13Page(qp);//查第i页
					//第i页写到文件
					exp.export(page.getDataList());
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			//结束写入
			exp.end();
		}
		return exp.getFileName();//返回文件名（含扩展名）
	}
	/**
	 * 查询未通过的记录总数
	 * @author zhangxr 2010-10-27
	 * */
	public DataCheck getNotPassed(){
		try {
			ETLContextVar var = etlContextVarDAO.findByName("BUSI_DATE");
			int sjrq = Integer.parseInt(var.getVarValue());
			return dataCheckDAO.findNotPassed(sjrq);
		} catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
